﻿#include"contact.h"

void init_con(con* pcon)
{
	assert(pcon);
	pcon->sz = 0;
	pcon->capacity = INIT_NUM;
	pcon->p=(struct people*)calloc( INIT_NUM,sizeof(struct people));
	if (pcon->p == NULL)
	{
		perror("init_con");
		return;
	}
	FILE* pf = fopen("co.txt", "rb");
	
	if (pf == NULL)
	{
		printf("没有文件");
		perror("fopen");
		return;
	}
	
	struct people ret = { 0 };
	while (fread(&ret, sizeof(struct people), 1, pf))
	{
		if (pcon->sz == pcon->capacity)
		{
			struct people* ptr = realloc(pcon->p, (pcon->capacity + ADD_NUM) * sizeof(struct people));
			if (ptr == NULL)
			{
				pcon->p = ptr;
				ptr = NULL;
				pcon->capacity += ADD_NUM;
			}
			else
			{
				printf("通讯录容量已经达到极限！无法增加！即将返回主菜单！\n");
				return;
			}
		}
		pcon->p[pcon->sz++] = ret;

	}
	fclose(pf);
	pf = NULL;

}
void menu()
{
	printf("%s\n","*********-welcom to telephone book!-***********");
	printf("%s\n","*********  1.add        2.del       ***********");
	printf("%s\n","*********  3.research   4.modify    ***********");
	printf("%s\n","*********  5.show       6.sort      ***********");
	printf("%s\n","*********  0.exit                   ***********");
	printf("%s\n","**********   请输入你的选择：>      ***********");
}
void add(con* pcon)
{
	assert(pcon);
	system("cls");
	if (pcon->sz == pcon->capacity)
	{
		struct people* ptr = realloc(pcon->p, (pcon->capacity + ADD_NUM) * sizeof(struct people));
		if (ptr == NULL)
		{
			pcon->p = ptr;
			ptr = NULL;
			pcon->capacity += ADD_NUM;
		}
		else
		{
			printf("通讯录容量已经达到极限！无法增加！即将返回主菜单！\n");
			return;
		}
	}
	struct people* ret = &pcon->p[pcon->sz];
	printf("请输入添加人的姓名：>\n");
	scanf("%s",ret->name);
	printf("请输入添加人的性别：>\n");
	scanf("%s", ret->sex);
	printf("请输入添加人的年龄：>\n");
	scanf("%d", &ret->age);
	printf("请输入添加人的电话：>\n");
	scanf("%s", ret->tel);
	printf("请输入添加人的住址：>\n");
	scanf("%s", ret->addr);
	printf("添加信息成功！\n");
	pcon->sz++;

}
void del(con* pcon)
{
	assert(pcon);
	system("cls");
	if (pcon->sz == 0)
	{
		printf("电话簿为空，无法删除，正在返回主菜单！\n");
		return;
	}
	char str[MAX_NAME];

	printf("请输入要删除人的性名：>\n");
	scanf("%s", str);
	int ret = search_name(pcon, str);
	if (ret == -1)
	{
		printf("要删除人的性名不存在！正在返回主菜单！\n");
		return;
	}
	else
	{
		memmove(&(pcon->p)[ret], &(pcon->p)[ret+1],((pcon->sz) - ret-1) *sizeof(struct people));
	}
	pcon->sz--;
	printf("删除信息成功！\n");
	

}
int search_name(con* pcon, char* str)
{
	for (int i = 0; i < pcon->sz; i++)
	{
		if (strcmp((pcon->p[i]).name, str) == 0)
			return i;
	}
	return -1;

}
void research(con* pcon)
{
	system("cls");
	char str[MAX_NAME];
	printf("请输入查找人的性名：>\n");
	scanf("%s", str);
	int ret = search_name(pcon, str);
	if (ret == -1)
	{
		printf("搜索人的性名不存在！正在返回主菜单！\n");
		return;
	}
	else
	{
		struct people peo = (pcon->p)[ret];
		printf("name\t\t\tsex\t\tage\t\ttel\t\t\taddress\t\t\t\n");
		printf("%-23s %-15s% -16d%  -10s%-20s\n", peo.name, peo.sex, peo.age, peo.tel, peo.addr);
	}
}
void show(con* pcon)
{
	if (pcon->sz == 0)
	{
		printf("电话簿为空，正在返回主菜单！");
		return;
	}
	system("cls");
	printf("%-23s %-15s% -16s%  -18s%-27s\n ", "name", "sex", "age", "tel", "addr");
	for (int i = 0; i < pcon->sz; i++)
	{
		struct people peo = (pcon->p)[i];
		
		printf("%-23s %-15s% -16d%  -18s%-27s\n ", peo.name, peo.sex, peo.age, peo.tel, peo.addr);
	}

}
void modify(con* pcon)
{
	system("cls");
	char str[MAX_NAME];
	printf("请输入修改人的性名：>\n");
	scanf("%s", str);
	int ret1 = search_name(pcon, str);
	if (ret1 == -1)
	{
		printf("要修改人的性名不存在！正在返回主菜单！\n");
		return;
	}
	else
	{
		struct people* ret = &(pcon->p)[ret1];
		printf("请输入要修改人的新姓名：>\n");
		scanf("%s", ret->name);
		printf("请输入要修改人的新性别：>\n");
		scanf("%s", ret->sex);
		printf("请输入要修改人的新年龄：>\n");
		scanf("%d", &ret->age);
		printf("请输入要修改人的新电话：>\n");
		scanf("%s", ret->tel);
		printf("请输入要修改人的新住址：>\n");
		scanf("%s", ret->addr);
		printf("修改信息成功！\n");
	}

}
int cmp(const void*e1, const void*e2)
{
	struct people* p1 = (struct people*)e1;
	struct people* p2 = (struct people*)e2;
	return strcmp(p1->name, p2->name);
}
void sort(con* pcon)
{
	system("cls");
	qsort(pcon->p, pcon->sz, sizeof(struct people), cmp);
	printf("排序成功！\n");

}

void destroy(con* pcon)
{
	FILE* pf = fopen("co.txt", "wb");
	if (pf == NULL)
	{
		perror("fopen");
		return;
	}
	if (fwrite(pcon->p, sizeof(struct people), pcon->sz, pf) != pcon->sz)
	{
		perror("fwriter");
	}
	fclose(pf);
	pf = NULL;
	free(pcon->p);
	pcon->p = NULL;
	pcon->capacity = 0;
	pcon->sz = 0;
}